# 2018-01-19-1638

$Source = "D:\Desktop", "D:\Documents", "D:\Music", "D:\Videos\��������"
$Destination = "D:\FileBackup\"
$LogDirectory = "LogFiles"
$RecycleBin = "Recycle Bin"


## log
if (!(Test-Path $Destination$LogDirectory)) {
    New-Item -Path $Destination$LogDirectory -ItemType "directory"
}

# create log file
$LogFile = New-Item -Path $Destination$LogDirectory -Name ('{0}.{1}' -f (Get-Date -Format "yyyy-MM-dd-HHmmss"), "log") -ItemType "file" -Verbose

Start-Transcript $LogFile


## backup
"Backup..." 

foreach ($s in $Source) {

    # source item exists
    if (Test-Path $s) {
        $source_item = (Get-Item $s)

        # source item is directory
        if (Test-Path $s -PathType Container) {
            $source_destination = "{0}{1}" -f $Destination, $source_item.Root.Name.Replace(":", "")
            # destination directory
            $dd = $source_item.FullName.Replace($source_item.Root, $source_destination)
            
            # destination directory not exists
            if (!(Test-Path $dd)) {
                New-Item -Path $dd -ItemType "directory" -Verbose
            }

            Get-ChildItem $s -Recurse |
                ForEach-Object {
    
                # child item is directory
                if (Test-Path $_.FullName -PathType Container) {
                    $d = ($_.FullName).Replace(($_.Root), $source_destination)
    
                    # sub-directory not exists
                    if (!(Test-Path $d)) {
                        New-Item -Path $d -ItemType "directory"
                    }
                }
                # child item is file
                elseif (Test-Path $_.FullName -PathType Leaf) {
                    $d = ($_.FullName).Replace(($_.Directory.Root), $source_destination)
                
                    # destination file exists
                    if (Test-Path $d) {
                        if ($_.LastWriteTime -gt (Get-item $d).LastWriteTime) {
                            Copy-Item -Path $_.FullName -Destination $d -Force -Verbose
                        }
                    }
                    # destination file not exists
                    else {
                        Copy-Item -Path $_.FullName -Destination $d -Verbose
                    }
                }
            }
        }
        # source item is file
        elseif (Test-Path $s -PathType Leaf) {
            $source_destination = "{0}{1}" -f $Destination, $source_item.Directory.Root.Name.Replace(":", "")
            # destination file
            $d = $source_item.FullName.Replace(($source_item.Directory.Root), $source_destination)
                
            # destination file exists
            if (Test-Path $d) {
                if ($source_item.LastWriteTimeUtc -gt (Get-item $d).LastWriteTimeUtc) {
                    Copy-Item -Path $source_item.FullName -Destination $d -Force -Verbose
                }
            }
            # destination file not exists
            else {
                Copy-Item -Path $source_item.FullName -Destination $d -Verbose
            }
        }
    }
}


## clean
"Cleaning..."

if (!(Test-Path $Destination$RecycleBin)) {
    New-Item -Path $Destination$RecycleBin -ItemType "directory"
}

Get-ChildItem -Path $Destination -Exclude $LogDirectory, $RecycleBin -Recurse | 
    Where-Object {($_.DirectoryName -notlike ($Destination + $LogDirectory + "*")) -and ($_.FullName -notlike ($Destination + $RecycleBin + "*"))} |
    ForEach-Object {
    $s = $_.FullName.Replace($Destination, "").Insert(1, ":")
    # source file not exists
    if (!(Test-Path $s)) {
        # recycle destination directory
        $rd = "{0}{1}\{2}" -f $Destination, $RecycleBin, (Get-Date -Format "yyyy-MM-dd-HHmmss")
        if (!(Test-Path $rd)) {
            New-Item -Path $rd -ItemType "directory"
        }
        Move-Item -Path $_.FullName -Destination $rd -Force -Verbose
    }
}

Stop-Transcript
